<?php
/***************************************************************************
 *
 *   copyright            : (C) 2005 Winds of Storm
 *
 *   $Id: transcripts.php,v 1.13 2007/11/08 20:45:24 amorgan Exp $
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

require("config.php");
$full_header	= 1;
$body['class']	= 'row2';
require($libdir . "init." . $phpEx);
require($libdir . "rooms." . $phpEx);
require($libdir . "chat_functs." . $phpEx);
require($libdir . "login_code." . $phpEx);
require($libdir . "transcripts." . $phpEx);

//print("<!-- op == {$_REQUEST['op']}, roomlist = {$_REQUEST['roomlist']} -->\n");

if ($_REQUEST['op'] == "") {
	//print("<!-- showing login, empty op -->\n");
	show_login("");
	exit;
}

if ($_REQUEST['roomlist'] == '' && $_REQUEST['room_name'] == '') {
	//print("<!-- showing login, empty roomlist -->\n");
	show_login("Please select a valid room.");
	exit;
}

if ($_REQUEST['room_name'] != '') { 
	//print("<!-- getting room info for {$_REQUEST['room_name']}... -->\n");
	$room	= getRoomInfoByName($_REQUEST['room_name']);
	$rid	= $room['room_id'];
} else {
	//print("<!-- getting room info for {$_REQUEST['roomlist']}... -->\n");
	$room   = getRoomInfo($_REQUEST['roomlist']);
	$rid	= $_REQUEST['roomlist'];
}

if ($rid == '') {
	show_login("Please select a valid room.");
	exit;
}

//print("<!-- checking authorization... rid = $rid-->\n");
auth_login($room);
//print("<!-- after authorization... -->\n");

$start	= "'{$_REQUEST['year1']}-{$_REQUEST['month1']}-{$_REQUEST['day1']} {$_REQUEST['hour1']}:{$_REQUEST['minute1']}:00'";
$end	= "'{$_REQUEST['year2']}-{$_REQUEST['month2']}-{$_REQUEST['day2']} {$_REQUEST['hour2']}:{$_REQUEST['minute2']}:59'";

$sql	= "SELECT
			*,DATE_FORMAT(event_date, '%m/%d %H:%i') AS event_date_fmt
		FROM
			{$dbprefix}events
		WHERE
			room_id_to=$rid AND
			(UNIX_TIMESTAMP(event_date) > UNIX_TIMESTAMP($start)) AND
			(UNIX_TIMESTAMP(event_date) < UNIX_TIMESTAMP($end)) AND
			event_chat_flags != 4
		ORDER BY
			event_id";
$str_sql = ereg_replace("\n", " ", $sql);
$str_sql = ereg_replace("\t", "", $str_sql);

print("<!-- SQL: $str_sql -->\n");

if (!($res = $db->sql_query($sql))) {
	$err	= $db->sql_error();
	showFatalError("<B>Error #{$err['code']}: {$err['message']}</b>: $str_sql");
}

$template = new Template($config['TPL_DIR']);
$template->set_filenames(array('body' => 'transcript_body.tpl'));

$vars	= array();
while (list($k, $v) = each($room)) {
	$vars[strtoupper($k)] = $v;
}
$vars['DATE1']	= stripslashes($start);
$vars['DATE2']	= stripslashes($end);
$template->assign_vars($vars);

while ($row = $db->sql_fetchrow($result)) {
	print("<!-- id = {$row['event_id']} -->");
	if ($_REQUEST['show_timestamps'] == 'on') {
		//print("[" . $row['event_date_fmt'] . "] ");
		$date_fmt	= "[" . $row['event_date_fmt'] . "] ";
	}
	//print(stripslashes($row['event_text']) . "</i></b></font><br />\n");

	$template->assign_block_vars('events', array(
		'EVENT_ID'	=> $row['event_id'],
		'EVENT_DATE'	=> $date_fmt,
		'EVENT_TEXT'	=> stripslashes($row['event_text'])
	));
}

$template->pparse('body');
